package com.tju.resource.server.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tju.resource.mapper.TbMatlRevMapper;
import com.tju.resource.model.TbMatlRev;
import com.tju.resource.server.TbMatlRevService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.QuickQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.tju.resource.tool.tool.QuickQuery.page;
import static com.tju.resource.tool.tool.QuickQuery.param;
import static com.tju.resource.tool.tool.QuickQuery.QueryType.*;

/**
 *
 */
@Service
public class TbMatlRevServiceImpl implements TbMatlRevService {

    @Autowired
    TbMatlRevMapper mapper;

    @Override
    public Result<Object> byPage(Map<String, String> params) {
        try {
            int page = Integer.parseInt(params.get("page"));
            int limit = Integer.parseInt(params.get("limit"));
            IPage<TbMatlRev> result = page(
                    mapper
                    , page
                    , limit
                    , param(EQUALS, "matl_id", params.get("matlId"))
                    , param(LIKE, "matl_rev", params.get("matlRev")));

            return ResultUtil.success(ResultEnum.SUCCESS, result.getRecords(), result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

    @Override
    public Result<Object> byMatl(Long matlId) {
        try {
            List<TbMatlRev> list = QuickQuery.list(mapper, "matl_id", matlId + "");
            return ResultUtil.success(ResultEnum.SUCCESS, list);
        } catch (Exception e) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

    @Override
    public Result<Object> byMatlRev(Long matlId, String rev) {
        try {
            TbMatlRev byOne = QuickQuery.byOne(mapper, "matl_id", rev);
            return ResultUtil.success(ResultEnum.SUCCESS, byOne);
        } catch (Exception e) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

    @Override
    @Transactional
    public Result<Object> add(TbMatlRev rev) {
        try {
            int insert = mapper.insert(rev);

            if (insert > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "版本添加成功!");
            } else {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "版本添加失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

    @Override
    @Transactional
    public Result<Object> upd(TbMatlRev rev) {
        try {
            int insert = mapper.updateById(rev);

            if (insert > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "版本修改成功!");
            } else {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "版本修改失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

    @Override
    @Transactional
    public Result<Object> del(Long rev) {
        try {
            int insert = mapper.deleteById(rev);

            if (insert > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "版本删除成功!");
            } else {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "版本删除失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

    @Override
    @Transactional
    public Result<Object> handle(JSONObject data) {
        try {
            JSONArray addData = data.getJSONArray("addData");
            JSONArray editData = data.getJSONArray("editData");
            JSONArray delData = data.getJSONArray("delData");

            addData.forEach(d -> {
                TbMatlRev rev = JSONUtil.toBean(JSONUtil.parseObj(d), TbMatlRev.class);

                rev.setMatlId(data.getLong("matlId"));
                add(rev);
            });
            editData.forEach(d -> {
                TbMatlRev rev = JSONUtil.toBean(JSONUtil.parseObj(d), TbMatlRev.class);

                rev.setMatlId(data.getLong("matlId"));
                upd(rev);
            });
            delData.forEach(d -> del(Long.parseLong(d + "")));

            return ResultUtil.success(ResultEnum.SUCCESS, "操作完成!");
        } catch (Exception e) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, String.format("操作失败(%s)", e));
        }

    }
}




